package com.ctgu.chatapp.view.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ctgu.chatapp.R
import com.ctgu.chatapp.databinding.ItemFriendBinding
import com.ctgu.chatapp.databinding.ItemFriendgroupBinding
import com.ctgu.chatapp.showPojo.Contact
import com.ctgu.chatapp.showPojo.FriendListByGroup

class FriendGroupAdapter(
    var data: MutableList<Any>, val context: Context, val chatID: Int, val profile: String
) :
    View.OnClickListener, RecyclerView.Adapter<FriendGroupAdapter.ViewHolder>() {

    lateinit var recyclerView: RecyclerView

    class ViewHolder(val binding: ViewBinding, int: Int) : RecyclerView.ViewHolder(
        if (int == 1) {
            (binding as ItemFriendBinding).root
        } else
            (binding as ItemFriendgroupBinding).root
    )

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ) = run {
        if (viewType == R.layout.item_friend) {
            val aBinding = ItemFriendBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            ViewHolder(aBinding, 1)
        } else {
            val bBinding =
                ItemFriendgroupBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            bBinding.groupDivider.setOnClickListener(this)
            ViewHolder(bBinding, 2)
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (data[position]) {
            is Contact -> {
                (holder.binding as ItemFriendBinding).contact = data[position] as Contact
                holder.binding.chatID = chatID
                holder.binding.tvIndex.visibility = View.GONE
                holder.binding.profile = profile
            }
            else -> {
                (holder.binding as ItemFriendgroupBinding).group =
                    data[position] as FriendListByGroup
            }
        }
    }

    override fun getItemCount() = data.size

    override fun getItemViewType(position: Int) = run {
        when (data[position]) {
            is FriendListByGroup -> R.layout.item_friendgroup
            else -> R.layout.item_friend
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        this.recyclerView = recyclerView
    }

    private fun addAll(position: Int, m_data: Collection<Contact>) {
        data.addAll(position, m_data.toMutableList())
        notifyItemRangeInserted(position, m_data.size)
    }

    private fun removeAll(position: Int, m_data: Collection<Contact>) {
        data.removeAll(m_data.toMutableList())
        notifyItemRangeRemoved(position, m_data.size)
    }

    override fun onClick(p0: View?) {
        val position = this.recyclerView.getChildAdapterPosition(p0!!)
        val o = this.data[position]
        if (o is FriendListByGroup) {
            val holder = this.recyclerView.getChildViewHolder(p0) as ViewHolder
            if (!o.expanding) {
                o.expanding = true
                (holder.binding as ItemFriendgroupBinding).arrowIcon.iconText =
                    p0.resources.getText(R.string.ic_keyboard_arrow_down)
                addAll(position + 1, o.friendList)
            } else {
                o.expanding = false
                (holder.binding as ItemFriendgroupBinding).arrowIcon.iconText =
                    p0.resources.getText(R.string.ic_keyboard_arrow_right)
                removeAll(position + 1, o.friendList)
            }
        }
    }

    fun changeData(data: List<Any>) {
        this.data = data.toMutableList()
        notifyDataSetChanged()
    }

}